﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Training.Dia1.Api.Models;
using Training.Dia1.Data.Managers;

using System.Threading.Tasks;
using System.Web.Http.Description;
using Training.Dia1.Data.Entities;

namespace Training.Dia1.Api.Controllers
{
    public class MeasureUnitController : ApiController
    {
        // GET api/MeasureUnit/
        public IEnumerable<MeasureUnitModel> Get()
        {
            return MeasureUnitManager.GetList("").Select(t => new MeasureUnitModel
                {
                     Id=t.Id,
                    Name=t.Name,
                    Abbreviation=t.Abbreviation,
                });
        }

        // GET api/MeasureUnit/5
        //[ResponseType(typeof(MeasureUnitModel))]
        public MeasureUnitModel Get(Guid id)
        {
            MeasureUnit t =  MeasureUnitManager.GetById(id);
            return new MeasureUnitModel {
                Id = t.Id,
                Name = t.Name,
                Abbreviation = t.Abbreviation,
            };
        }
        //[ResponseType(typeof(MeasureUnitModel))]
        public MeasureUnitModel Post(MeasureUnitModel m)
        {
            MeasureUnit t = MeasureUnitManager.Create(new MeasureUnit
            {
                Name = m.Name,
                Abbreviation = m.Abbreviation,
            });
            return new MeasureUnitModel
            {
                Id = t.Id,
                Name = t.Name,
                Abbreviation = t.Abbreviation,
            };
        }


        public MeasureUnitModel Put(Guid id, [FromBody] MeasureUnitModel m)
        {
            MeasureUnit t = MeasureUnitManager.Update(new MeasureUnit
            {
                Id=id,
                Name = m.Name,
                Abbreviation = m.Abbreviation,
            });
            
            return new MeasureUnitModel
            {
                Id = t.Id,
                Name = t.Name,
                Abbreviation = t.Abbreviation,
            };
             
            //return new HttpResponseMessage(HttpStatusCode.OK);
        }
        public void DeleteXxxx(Guid id)
        {
            MeasureUnitManager.Delete(id);
        }


    }
}
